﻿using AutoMapper;
using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Read.Api.Application.Commands.OrdersCommands;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Read.Api.Application.CommandsHander.OrdersCommandsHander
{
    public class ShowOrdersCommandHander : IRequestHandler<ShowOrdersCommand, APIResult<List<OrdersModel>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public ShowOrdersCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<List<OrdersModel>>> Handle(ShowOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<OrdersModel>> result = new APIResult<List<OrdersModel>>();

            var list = await db.Queryable<OrdersModel>()
                                .Where(x => x.Del == false && x.DocumentsId == request.DocumentsId)
                                .ToListAsync();

            result.Data = list;

            result.Code = APIEnum.success;
            result.Msg = "查询成功";

            return await Task.FromResult(result);
        }
    }
}
